#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "BorderItems.h"

#include "Class.h"
#include "String.h"
#include "Look.h"
#include "TextItem.h"
#include "Math.h"

//---- Matte -------------------------------------------------------------------

NewMetaImpl(Matte,CompositeVObject, (T(border)));

Matte::Matte(VObject *in) : CompositeVObject(cIdNone, in, 0)
{
	border= gPoint16;
}

Matte::Matte(const Point &b, VObject *in) : CompositeVObject(cIdNone, in, 0)
{
	border= b;
}

Matte::Matte(int id, const Point &b, VObject *in) : CompositeVObject(id, in, 0)
{
	border= b;
}

void Matte::SetBorder(const Point &b)
{
	border= b;
}

void Matte::SetOrigin(Point at)
{
	VObject::SetOrigin(at);
	At(0)->SetOrigin(at+border);
}

void Matte::SetExtent(Point e)
{
	VObject::SetExtent(e);
	e-= 2*border;
	At(0)->SetExtent(e);
}

Metric Matte::GetMinSize()
{
	VObject *inner= At(0);
	Metric m= At(0)->GetMinSize();
	SetFlag(eVObjHFixed, inner->TestFlag(eVObjHFixed));
	SetFlag(eVObjVFixed, inner->TestFlag(eVObjVFixed));
	return m.Expand(border);
}

OStream& Matte::PrintOn(OStream &s)
{
	CompositeVObject::PrintOn(s);
	return s << border SP;
}

IStream& Matte::ReadFrom(IStream &s)
{
	CompositeVObject::ReadFrom(s);
	return s >> border;
}

//---- BorderItem -------------------------------------------------------------------

NewMetaImpl0(BorderItem,CompositeVObject);

BorderItem::BorderItem(VObject *in) : CompositeVObject(cIdNone, in, 0)
{
}

BorderItem::BorderItem(VObject *ti, VObject *in)
					: CompositeVObject(cIdNone, in, ti, 0)
{
}

BorderItem::BorderItem(const char *ti, VObject *in)
					: CompositeVObject(cIdNone, in, new TextItem(ti), 0)
{
}

void BorderItem::SetTitle(const char *title, bool redraw)
{
	if (Size() >= 2) {
		TextItem *ti= (TextItem*) At(1);
		if (ti->IsKindOf(TextItem))
			ti->SetString(title, redraw);
		else
			Warning("SetTitle", "title not a TextItem");
	} else
		Warning("SetTitle", "no title");
}

void BorderItem::SetOrigin(Point at)
{
	VObject::SetOrigin(at);
	gLook->GroupLayout()->SetOrigin(this, at);
}

void BorderItem::SetExtent(Point e)
{
	VObject::SetExtent(e);
	gLook->GroupLayout()->SetExtent(this, e);
}

Metric BorderItem::GetMinSize()
{
	Metric m(gLook->GroupLayout()->GetMinSize(this));
	SetFlag(eVObjHFixed, At(0)->TestFlag(eVObjHFixed));
	SetFlag(eVObjVFixed, At(0)->TestFlag(eVObjVFixed));
	return m;
}

void BorderItem::Draw(Rectangle r)
{
	CompositeVObject::Draw(r);
	gLook->GroupLayout()->Adorn(this, r, 0);
}

//---- BackgroundItem ----------------------------------------------------------

NewMetaImpl(BackgroundItem,CompositeVObject, (TP(ink)));

BackgroundItem::BackgroundItem(Ink *i, VObject *ip)
											: CompositeVObject(cIdNone, ip, 0)
{
	ink= i;
	SetFlag(eVObjHFixed | eVObjVFixed);
}

void BackgroundItem::DrawInner(Rectangle r, bool highlight)
{
	GrPaintRect(r, ink);
	CompositeVObject::DrawInner(r, highlight);
}

void BackgroundItem::SetOrigin(Point at)
{
	VObject::SetOrigin(at);
	At(0)->SetOrigin(at);
}

void BackgroundItem::SetExtent(Point e)
{
	VObject::SetExtent(e);
	At(0)->SetExtent(e);
}

OStream& BackgroundItem::PrintOn(OStream &s)
{
	CompositeVObject::PrintOn(s);
	return s << ink SP;
}

IStream& BackgroundItem::ReadFrom(IStream &s)
{
	CompositeVObject::ReadFrom(s);
	return s >> ink;
}
